import axios from 'axios'
// import { showToast, Dialog } from 'vant'
// import { reLogin } from '@/utils/login'
// create an axios instance
const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL, // url = base url + request url
  withCredentials: true, // withCredentials指示是否有跨站点访问控制请求
  timeout: 30000 // request timeout
})

// request interceptor 请求拦截
service.interceptors.request.use(
  (config) => {
    if (config.url.indexOf('http://') !== -1 || config.url.indexOf('https://') !== -1) {
      config.baseURL = ''
    }
    return config
  },
  (error) => {
    console.log(error)
    return Promise.reject(error)
  }
)
// response interceptor 返回拦截
service.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.log('err' + error) // for debug
    // 处理超时,重试一次
    if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
      error.message = '请求超时，请稍后重试'
    }
    return Promise.reject(error)
  }
)
export default service
